<template>
  <a-modal :title="title" :width="900" :visible="visible" :confirmLoading="confirmLoading" @ok="handleOk" @cancel="handleCancel" cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="model" :rules="validatorRules">
        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name" label="名称">
          <a-input placeholder="请输入名称" v-model="model.name" />
        </a-form-model-item>
        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="code" label="数据值">
          <a-input placeholder="请输入数据值，唯一" v-model="model.code" />
        </a-form-model-item>
        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="description" label="描述">
          <a-textarea rows="5" placeholder="请输入描述" v-model="model.description" />
        </a-form-model-item>
        <!-- <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="描述">
          <a-input v-model="model.description" />
        </a-form-model-item>

        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="排序值">
          <a-input-number :min="1" v-model="model.sortOrder" />
          值越小越靠前
        </a-form-model-item> -->

        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="是否启用" hasFeedback>
          <a-switch checkedChildren="启用" unCheckedChildren="禁用" @change="onChose" v-model="visibleCheck" />
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import { attrCreateOrUpdateValue } from '@/api/box/setting'
import { getAction } from '@api/manage'

export default {
  name: 'DictItemModal',
  data() {
    return {
      title: '操作',
      visible: false,
      visibleCheck: true,
      model: {},
      dictId: '',
      status: 1,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {
        name: [{ required: true, message: '请输入名称!' }],
        code: [{ required: true, message: '请输入数据值!' }],
        description: [{ required: true, message: '请输入描述!' }]
      },
      columns: [],
      columnsList: []
    }
  },
  created() {},
  methods: {
    add(dictId) {
      this.dictId = dictId
      //初始化默认值
      this.edit({})
    },
    edit(record) {
      if (record.id) {
        this.dictId = record.dictId
      }

      this.visibleCheck = record.isactive == 'Y' ? true : false
      this.model = Object.assign({}, record)
      this.model.attrId = this.dictId
      this.visible = true
    },
    onChose(checked) {
      if (checked) {
        this.status = 1
        this.visibleCheck = true
      } else {
        this.status = 0
        this.visibleCheck = false
      }
    },
    // 确定
    handleOk() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          this.model.name = (this.model.name || '').trim()
          this.model.code = (this.model.code || '').trim()
          this.model.description = (this.model.description || '').trim()
          this.model.isactive = this.visibleCheck ? 'Y' : 'N'
          let params = {
            requestData: this.model
          }
          attrCreateOrUpdateValue(params)
            .then(res => {
              if (res.code == 0) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
            })
        } else {
          return false
        }
      })
    },
    // 关闭
    handleCancel() {
      this.close()
    },
    close() {
      this.$emit('close')
      this.visible = false
      this.$refs.form.resetFields()
    },
    filterOption(input, option) {
      return option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
    }
  }
}
</script>

